﻿''' <summary>
''' Represents an entry in a processed command line's event log.
''' </summary>
''' <remarks></remarks>
Public Structure LogEntry
    Public ReadOnly Id As Guid
    Public ReadOnly Level As Integer
    Public ReadOnly Source As String
    Public ReadOnly [Class] As String
    Public ReadOnly [Date] As Date
    Public ReadOnly Code As Integer
    Public ReadOnly Message As String

    Public Sub New(entryLevel As Integer, entrySource As String, entryClass As String, entryCode As Integer, entryMessage As String)
        Id = Guid.NewGuid
        Level = entryLevel
        Source = entrySource
        [Class] = entryClass
        [Date] = Now
        Code = entryCode
        Message = entryMessage
    End Sub

    Public Function ToFormattedString() As String
        Dim color As ConsoleColor = ConsoleColor.Gray
        If Level = CommandInterpreter.LogLevelInformation Then
            color = ConsoleColor.Cyan
        ElseIf Level = CommandInterpreter.LogLevelWarning Then
            color = ConsoleColor.Yellow
        ElseIf Level = CommandInterpreter.LogLevelError Then
            color = ConsoleColor.Red
        End If
        Dim colorString As String = ConsoleOutput.GetColorFormatCode(color)
        Dim baseString As String = String.Concat([Date].ToString("MM/dd/yy hh:mm:ss tt"), ":", Source, "-", [Class], ":", Code, ">")
        Dim maxLength As Integer = Console.WindowWidth - baseString.Length
        If maxLength > 0 Then
            If maxLength >= Message.Length Then
                baseString &= Message
            Else
                baseString &= Message.Substring(0, maxLength - 4) & "..."
            End If
        Else
            baseString = baseString.Substring(0, Console.WindowWidth - 4) & "..."
        End If
        Return String.Concat(colorString, baseString)
    End Function
End Structure